//
//  VideoViewController.m
//  FitTime
//
//  Created by 赵建平 on 16/3/30.
//  Copyright © 2016年 赵建平. All rights reserved.
//

#import "VideoViewController.h"
#import "V_VideoTableViewCell.h"
#import "PlanDetailsViewController.h"
#import "ProgramDailyListModel.h"

@interface VideoViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *videoModelArray;//装model的数组
@property (nonatomic, strong) NSMutableArray *mutableArray;
@property (nonatomic, strong) NSMutableArray *IDArray;

@end

@implementation VideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor redColor];
    self.navigationController.navigationBar.translucent = NO;
    
    
    
    [self createTableView];
    [self getData];
}

#pragma mark - 创建tableView
- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 113, 0);
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.bounces = NO;
    
    [self.tableView registerClass:[V_VideoTableViewCell class] forCellReuseIdentifier:@"V_VideoTableViewCellIdentifier"];
    
    [self.view addSubview:self.tableView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.videoModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    V_VideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"V_VideoTableViewCellIdentifier"];
    
    VideoModel *videoModel = self.videoModelArray[indexPath.row];
    
    cell.videoModel = videoModel;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PlanDetailsViewController *plan = [[PlanDetailsViewController alloc] init];
    VideoModel *videoModel = self.videoModelArray[indexPath.row];
    plan.videoModel = videoModel;
    
    self.IDArray = [NSMutableArray arrayWithCapacity:0];
    for (NSDictionary *dic in videoModel.programDailyList) {
        ProgramDailyListModel *model1 = [[ProgramDailyListModel alloc] initWithDic:dic];
        [self.IDArray addObject:model1.ID];
    }
    
    plan.IDArray = self.IDArray;
    
    [self.navigationController pushViewController:plan animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return SCREENHEIGHT / 3;
}



#pragma mark - 数据解析
- (void)getData {
    
   
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
   [ZJPAFNetTool GETNetWithUrl:@"http://api.fit-time.cn/ftvideo/getPrograms" body:nil headFile:nil response:ZJPJSON success:^(id result) {
      
       self.videoModelArray = [NSMutableArray arrayWithCapacity:0];
       for (NSDictionary *dic in result[@"programs"]) {
           VideoModel *model = [[VideoModel alloc] initWithDic:dic];
           [self.videoModelArray addObject:model];
       }
       
       dispatch_async(dispatch_get_main_queue(), ^{
           [hud hide:YES];
           [self.tableView reloadData];
       });
       
   } failure:^(NSError *error) {
       
   }];
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
